/**
 * @description base64转bloburl
 * @param {String} base64String base64字符串
 */
interface Base64ToBlobUrl {
	(base64String: string): Promise<string>;
}

const base64ToBlobUrl: Base64ToBlobUrl = base64String => {
	return new Promise((resolve, reject) => {
		try {
			// 移除"data:image/*;base64,"前缀
			const pureBase64 = base64String.replace(/^data:image\/[^;]+;base64,/, '');
			// 将Base64解码为Uint8Array
			const byteCharacters = atob(pureBase64);
			const byteNumbers = new Array(byteCharacters.length);
			for (let i = 0; i < byteCharacters.length; i++) {
				byteNumbers[i] = byteCharacters.charCodeAt(i);
			}
			const byteArray = new Uint8Array(byteNumbers);
			// 创建Blob
			const blob = new Blob([byteArray], { type: 'image/png' }); // 根据实际情况调整MIME类型
			// 创建并返回Blob URL
			const url = URL.createObjectURL(blob);
			resolve(url);
		} catch (e) {
			reject(e);
		}
	});
};
export default base64ToBlobUrl;
